<?php

/**
 * This is the model class for table "zy_activity".
 *
 * The followings are the available columns in table 'zy_activity':
 * @property integer $activity_id
 * @property integer $classify_id
 * @property string $title
 * @property string $activity_time
 * @property integer $provide_gift_number
 * @property integer $Used_gift_number
 * @property string $bak1
 * @property string $bak2
 * @property integer $is_delete
 * @property string $crate_time
 * @property string $crate_user
 * @property string $update_time
 * @property string $update_user
 */
class Activity extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Activity the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_activity';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('activity_code,title','unique'),
			array('title,activity_code,provide_gift_number,activity_time','required'),
			array('classify_id, provide_gift_number, Used_gift_number,user_id', 'numerical', 'integerOnly'=>true),
			array('title,activity_code', 'length', 'max'=>300),
			array('activity_time', 'length', 'max'=>24),
			array('address1,address2,address3', 'length', 'max'=>200),
			array('bak1, bak2', 'length', 'max'=>400),
			array('activity_out_time', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('activity_id, classify_id,activity_out_time,address1,address2,address3, title, activity_code,activity_time, provide_gift_number, Used_gift_number,user_id, bak1, bak2, is_delete, crate_time, crate_user, update_time, update_user', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'activity_id' => Yii::t('base','Activity Id'),
			'classify_id' => Yii::t('base','Activity Classify'),
			'title' => Yii::t('base','Title'),
			'activity_code'=>Yii::t('base','Activity Code'),
			'activity_time' => Yii::t('base','Activity Time'),
			'provide_gift_number' => Yii::t('base','Provide Gift Number'),
			'Used_gift_number' => Yii::t('base','Used Gift Number'),
			'activity_out_time'=>Yii::t('base','Activity Out Time'),
			'address1'=>Yii::t('base','Award Address'),
			'address2'=>Yii::t('base','Area'),
			'address3'=>Yii::t('base','Specific Address'),
			'user_id' => Yii::t('base','Partners'),
			'bak1' => 'Bak1',
			'bak2' => 'Bak2',
			'is_delete' => 'Is Delete',
			'crate_time' => 'Crate Time',
			'crate_user' => 'Crate User',
			'update_time' => 'Update Time',
			'update_user' => 'Update User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$level = yii::app()->user->level();
		$criteria=new CDbCriteria;

		$criteria->compare('activity_id',$this->activity_id);
		$criteria->compare('classify_id',$this->classify_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('activity_code',$this->activity_code,true);
		$criteria->compare('activity_time',$this->activity_time,true);
		$criteria->compare('provide_gift_number',$this->provide_gift_number);
		$criteria->compare('Used_gift_number',$this->Used_gift_number);
		if($level == 1){
			$criteria->compare('user_id',$this->user_id);
		}else{
			$criteria->compare('user_id',yii::app()->user->id);
		}
		$criteria->compare('activity_out_time',$this->activity_out_time);
		$criteria->compare('address1',$this->address1);
		$criteria->compare('address2',$this->address2);
		$criteria->compare('address3',$this->address3);
		$criteria->compare('bak1',$this->bak1,true);
		$criteria->compare('bak2',$this->bak2,true);
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('crate_time',$this->crate_time,true);
		$criteria->compare('crate_user',$this->crate_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user',$this->update_user,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	/**
	 * 获取活动分类
	 */
	 function getActivityClassifyName($num){
	 	if($num == ""){
	 		$sql="select * from zy_activity_classify where user_id='".yii::app()->user->id."'";
	 		$rs = yii::app()->db->createCommand($sql)->queryAll();
	 		return CHtml::listData($rs,'activity_classify_id','classify_name');
	 	}else{
	 		$sql="select * from zy_activity_classify where user_id='".yii::app()->user->id."' and Activity_classify_id ='".$num."'";
	 		$rs = yii::app()->db->createCommand($sql)->queryAll();
	 		return activityClassify::model()->getClassifyName($rs[0]['classify_name']);
	 	}
	 }
	 
	 //使用递归的方法获取不重复密码
	 function getPasswrd(){
	 	//range 是将1到9 列成一个数组 
		$numbers = range (1,9); 
		//shuffle 将数组顺序随即打乱 
		shuffle($numbers); 
		//array_slice 取该数组中的某一段 
		$no=6; 
		$result = array_slice($numbers,0,$no);
		for ($i=0;$i<$no;$i++){
			$passwrd.= $result[$i];
		}
		$sql="select password from zy_gift_password where password ='".$passwrd."'";
		$rs = yii::app()->db->createCommand($sql)->queryAll();
		if(!empty($rs)){
			$passwrd = $this->getPasswrd();
			return $passwrd;
		}else{
			return $passwrd;
		}
	 }
	 

	 //查询地址,返回list
	 function getAddressListActivity($num,$userId){
		
		$sql="select * from zy_award_address where address_id='".$num."' and  belong = '".$userId."'";
			
		$rs = yii::app()->db->createCommand($sql)->queryAll();

		return $rs[0]['address'];
		
	 }
	 
	 //查询地址,返回list
	 function getAddressList($num){
	 
	 	$userId = yii::app()->user->id;
	 
	 	if($num == 'falg'){
	 
	 		$sql="select * from zy_award_address where belong = '".$userId."'";
	 
	 		$rs = yii::app()->db->createCommand($sql)->queryAll();
	 
	 		return CHtml::listData($rs,'address_id','company_name');
	 
	 	}else{
	 
	 		$sql="select * from zy_award_address where address_id='".$num."' and  belong = '".$userId."'";
	 			
	 		$rs = yii::app()->db->createCommand($sql)->queryAll();
	 
	 		return $rs[0]['address'];
	 
	 	}
	 }
	 
	 //admin 用户
	 function getAddressLists($num,$userId){
	 
	 		$sql="select * from zy_award_address where address_id='".$num."' and  belong = '".$userId."'";
	 			
	 		$rs = yii::app()->db->createCommand($sql)->queryAll();
	 
	 		return $rs[0]['address'];
	 }
	 
	 //查询当前用户设置的地址城市
	 public function getCityAddress(){
	 	
	 	$userId = Yii::app()->user->id;
	 	
	 	$sql="select address_id,area from zy_award_address where belong='".$userId."'";
	 	
	 	$rs = yii::app()->db->createCommand($sql)->queryAll();
	 	
	 	//转换数组格式 array('1'=>'2','2'=>'3')
	 	$data = array();
	 	
	 	for($i=0; $i<count($rs); $i++){
	 		
	 		
	 		foreach ($rs[$i] as $key=>$value)
	 		{
	 			if($key == 'address_id'){
	 				$k=$value;
	 			}
	 			$data[$k]=$value;
	 		}
	 		
	 	}
	 	
	 	return $data;
	 }
	 
	 //查询当前活动设置的领奖地址
	 public function getAddress1($id){
	 	
	 	$sql="select address1 from zy_activity where activity_id='".$id."'";
	 	
	 	$rs = yii::app()->db->createCommand($sql)->queryAll();
	 	
	 	return $rs[0]['address1'];
	 }
}